package com.xwd.dao.impl;

import com.xwd.dao.DBUtils;
import com.xwd.dao.UsersDao;
import com.xwd.domain.Role;
import com.xwd.domain.Users;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UsersDaoImpl extends DBUtils implements UsersDao {

    String sql=null;

    @Override
    public Users login(String username, String password) {
        Users user = null;
        try {
            sql="select * from users where loginname=? and password=?";
            List params = new ArrayList();
            //存值
            params.add(username);
            params.add(password);
            resultSet = query(sql, params);
            if(resultSet==null){
                return null;
            }
            user = null;
            while (resultSet.next()) {
                user=new Users();
                user.setUserId(resultSet.getInt("userid"));
                user.setRealName(resultSet.getString("realname"));
                user.setLoginName(resultSet.getString("loginname"));
                user.setRoleId(resultSet.getInt("roleId"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return user;
    }

    @Override
    public List<Users> getUserlist(int pageIndex, int pageSize) {
        List<Users> usersList = new ArrayList<>();
        try {
            sql="SELECT userid,loginname,realname,rolename FROM users u ,role r WHERE u.roleid=r.roleid limit ?,?";
            List param = new ArrayList();
            param.add((pageIndex-1)*pageSize);
            param.add(pageSize);
            ResultSet resultSet = query(sql, param);
            while (resultSet.next()){
                Users users = new Users();
                users.setUserId(resultSet.getInt("userid"));
                users.setLoginName(resultSet.getString("loginname"));
                users.setRealName(resultSet.getString("realname"));
                Role role = new Role();
                role.setRoleName(resultSet.getString("rolename"));
                users.setRole(role);
                usersList.add(users);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return usersList;
    }

    @Override
    public int getTotle() {
        int totle=0;
        try {
            sql="SELECT count(*) FROM users u ,role r WHERE u.roleid=r.roleid";
            resultSet = query(sql, null);
            while (resultSet.next()){
                totle = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return totle;
    }

    @Override
    public int addUses(Users users) {
        int i=0;
        try {
            sql= "insert into users value(null,?,?,?,?,?,?,?,?,?,?)";
            List param = new ArrayList();
            param.add(users.getLoginName());
            param.add(users.getPassWord());
            param.add(users.getRealName());
            param.add(users.getSex());
            param.add(users.getEmail());
            param.add(users.getAddress());
            param.add(users.getPhone());
            param.add(users.getCardId());
            param.add(users.getDesc());
            param.add(users.getRoleId());
            i = update(sql, param);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return i;
    }
}
